package menu

import (
	"context"

	"mall/common/errorx"
	"mall/common/jwtx"
	"mall/service/backend/api/internal/svc"
	"mall/service/backend/api/internal/types"
	"mall/service/backend/rpc/backendclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateMenuLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUpdateMenuLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateMenuLogic {
	return &UpdateMenuLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UpdateMenuLogic) UpdateMenu(req *types.UpdateMenuRequest) (resp *types.UpdateMenuResponse, err error) {
	if req.Id == 0 {
		return nil, errorx.Error(errorx.ERROR_MENU_ID_REQUIRED, "")
	}

	adminId, _ := jwtx.GetUserId(l.ctx)

	_, err = l.svcCtx.BackendRpc.BackendUpdateMenu(l.ctx, &backendclient.UpdateMenuRequest{
		Id:        req.Id,
		Name:      req.Name,
		Pid:       req.ParentId,
		Status:    req.Status,
		MenuOrder: req.MenuOrder,
		UpdatedBy: adminId,
	})
	if err != nil {
		return nil, err
	}

	return &types.UpdateMenuResponse{
		Status:  "ok",
		Message: "更新成功",
	}, nil
}
